package com.fuze.config;

import com.fuze.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

/**
 * @author sepc_
 * @date 3/17/2021 09:29
 * Token拦截器注册配置
 */
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    /**
     * token拦截器
     */
    @Resource
    private TokenInterceptor tokenInterceptor;

    /**
     * 跨域处理拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器，要声明拦截器对象和要拦截的请求
        registry.addInterceptor(tokenInterceptor)
                //所有路径都被拦截
                .addPathPatterns("/**")
                // 排除请求
                .excludePathPatterns("/auth/login", "/user/register", "/admin/register")
                .excludePathPatterns("/doc.html", "/error")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")

        ;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}
